import { Toast } from 'vant'
import { ref, type Ref } from 'vue'
import { RequestSearchGoods } from "@/apis/searchGoods"
import type { IGoods } from "@/types/type"
export const useSearGoods = () => {
    const searchValue: Ref<string> = ref('') //搜索框
    const SearchGoods: Ref<Array<IGoods>> = ref([])//搜索后渲染页面的数据
    const state: Ref<boolean> = ref(false)
    const onCancel = () => {
        searchValue.value = ""//点击取消后把搜索框清空
        SearchGoods.value = []
        state.value = !state.value
        Toast('取消');
    }
    const onSearch = async (searchValue: any) => {
        const data = await RequestSearchGoods(searchValue)
        const { resultCode, resultInfo } = data
        if (resultCode === 1) {
            SearchGoods.value = resultInfo.list
            state.value = !state.value
        } else {
            Toast('搜索商品不存在......')
        }
    }
    return { searchValue, onSearch, onCancel, SearchGoods, state }
}